/*
 * MIT License
 *
 * Copyright (c) 2025 Mark·虎
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.hsxxherp.key.support.i18n.controller;

import com.hsxxherp.key.support.i18n.model.dto.I18nLocaleDTO;
import com.hsxxherp.key.support.i18n.model.entity.I18nLocale;
import com.hsxxherp.key.support.i18n.model.mapper.I18nLocaleMapper;
import com.hsxxherp.key.support.i18n.service.II18nLocaleService;
import com.hsxxherp.key.support.model.entity.AdvancedQuery;
import com.hsxxherp.key.support.model.entity.ApiBaseResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * i18n 语言代码管理控制器
 *
 * @author Mark·虎（2486970363@qq.com）
 * @since 2024-01-25
 */
@Tag(name = "I18nLocale 管理", description = "语言代码管理相关接口")
@RestController
@RequestMapping("/api/sys/i18n/locale")
public class I18nLocaleController {

    private final II18nLocaleService i18nLocaleService;

    public I18nLocaleController(II18nLocaleService i18nLocaleService) {
        this.i18nLocaleService = i18nLocaleService;
    }

    @Operation(summary = "注册语言代码", description = "用于注册新的语言代码信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "注册成功",
                    content = @Content(schema = @Schema(implementation = I18nLocale.class))),
            @ApiResponse(responseCode = "400", description = "请求参数错误", content = @Content),
            @ApiResponse(responseCode = "500", description = "服务器内部错误", content = @Content)
    })
    @PostMapping("/register")
    public ApiBaseResponse<I18nLocale> registerLocale(@RequestBody I18nLocaleDTO localeDTO) {
        I18nLocale locale = I18nLocaleMapper.INSTANCE.toEntity(localeDTO);
        I18nLocale i18nLocale = i18nLocaleService.registerLocale(locale);

        return ApiBaseResponse.success(i18nLocale);
    }

    @Operation(summary = "高级查询语言代码", description = "根据多个条件查询语言代码信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = I18nLocale.class))),
            @ApiResponse(responseCode = "400", description = "请求参数错误", content = @Content),
            @ApiResponse(responseCode = "500", description = "服务器内部错误", content = @Content)
    })
    @PostMapping("/query")
    public ApiBaseResponse<List<I18nLocale>> queryLocales(@RequestBody AdvancedQuery query) {
        List<I18nLocale> locales = i18nLocaleService.advancedQueryI18nLocales(query);
        return ApiBaseResponse.success(locales);
    }
}